package com.hmy.infrastructure.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.fasc.open.api.utils.crypt.FddCryptUtil;
import com.hmy.ccp.error.annotations.HmyGateWayController;
import com.hmy.infrastructure.common.constant.InfrastructureConstant;
import com.hmy.infrastructure.common.properties.FadadaProperties;
import com.hmy.infrastructure.service.signature.FadadaRevokeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 法大大回调处理接口
 * @Author seven.qi
 * @Date 2023/12/13 11:06
 * @Version 1.0
 */

@RestController
@Slf4j
@HmyGateWayController
public class FadadaRevokeController {

    @Resource
    private FadadaProperties fadadaProperties;

    @Resource
    private FadadaRevokeService fadadaRevokeService;

    @PostMapping("/revoke/event")
    public String revoke(@RequestParam("bizContent") String bizContent, HttpServletRequest request) throws Exception {

        String appSecret = fadadaProperties.getAppSecret();
        //获取请求头参数
        String appId = request.getHeader("X-FASC-App-Id");
        String signType = request.getHeader("X-FASC-Sign-Type");
        String sign = request.getHeader("X-FASC-Sign");
        String timestamp = request.getHeader("X-FASC-Timestamp");
        //事件名称，开发者可以根据不同事件名称去解析bizContent的值，实现不同的逻辑
        String event = request.getHeader("X-FASC-Event");
        String nonce = request.getHeader("X-FASC-Nonce");
        //验签
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("X-FASC-App-Id", appId);
        paramMap.put("X-FASC-Sign-Type", "HMAC-SHA256");
        paramMap.put("X-FASC-Timestamp", timestamp);
        paramMap.put("X-FASC-Nonce", nonce);
        paramMap.put("X-FASC-Event", event);
        paramMap.put("bizContent", bizContent);
        //参数排序，ascii码排序
        String sortParam = FddCryptUtil.sortParameters(paramMap);
        log.info("fadada revoke params : {}", JSONUtil.toJsonPrettyStr(paramMap));
        //生成签名后可以进行校验
        String signature = FddCryptUtil.sign(sortParam, timestamp, appSecret);
        if (signature.equals(sign)) {
            Object eventStr = JSONUtil.parseObj(bizContent).get(InfrastructureConstant.FADADA_SIGN_TASKID);
            if (event.equalsIgnoreCase(InfrastructureConstant.FADADA_SIGN_TASK_FINISHED) && ObjectUtil.isNotEmpty(eventStr)) {
                String signTaskId = JSONUtil.parseObj(bizContent).get(InfrastructureConstant.FADADA_SIGN_TASKID).toString();
                String transReferenceId = JSONUtil.parseObj(bizContent).get(InfrastructureConstant.FADADA_TRANS_REFERENCE_ID).toString();
                log.info("fadada task sign finish, signTaskId : {}, transReferenceId : {}", signTaskId, transReferenceId);
                fadadaRevokeService.signSuccess(signTaskId, transReferenceId);
            }
            return "{\"msg\":\"success\"}";
        }

        return "{\"msg\":\"success\"}";
    }
}
